/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista;

import GUI.GUI_Previsualizacion;
import GUI.InicioAplicacion;
import Modelo.Idiomas.LocaleManager;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.MediaTracker;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.ReplicateScaleFilter;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Antonio Ariza
 */
public class Previsualizacion extends Canvas implements Border {

    private Controlador.Controlador controlador;
    private Image mapa;
    private int despY;
    private int despX;
    private String titulo;
    private Image nomap;
    private Image original;
    private int contador;
    private JPanel displayMapa;
    private RenderingHints opciones = new RenderingHints(null);
    private JFileChooser browse;
    private String ruta;

    public Previsualizacion() {
        contador = 0;
        controlador = InicioAplicacion.getControlador();
        displayMapa = GUI_Previsualizacion.getDisplayMapa();
        despX = 0;
        despY = 0;
        titulo = LocaleManager.getMessage("previsualizar");
        opciones.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        opciones.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        opciones.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
        opciones.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        opciones.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        opciones.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        opciones.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
        opciones.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        browse = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & PNG", "JPG", "PNG");
        browse.setFileFilter(filter);

        int seleccion = browse.showOpenDialog(displayMapa);
        if (seleccion == browse.APPROVE_OPTION) {
            mapa = Toolkit.getDefaultToolkit().getImage(browse.getSelectedFile().getPath());
            original = Toolkit.getDefaultToolkit().getImage(browse.getSelectedFile().getPath());
            ruta = browse.getSelectedFile().getPath();
        }
        nomap = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Archivos/Imagenes/nomap.jpg"));
    }

    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        paintNotFound(g);
        //g.drawRegion(mapa, 0, 0, mapa.getWidth(), mapa.getHeight(), 0, despX,
        //        despY, 0);
        g.drawImage(mapa, despX, despY, displayMapa);
        //g.drawString(titulo, 5, 5);
        g.drawLine(getWidth() / 2 - 3, getHeight() / 2, getWidth() / 2 + 3, getHeight() / 2);
        g.drawLine(getWidth() / 2, getHeight() / 2 - 3, getWidth() / 2, getHeight() / 2 + 3);
        this.update(displayMapa.getGraphics());
        this.paint(displayMapa.getGraphics());
    }

    public Insets getBorderInsets(Component c) {
        return new Insets(0, 0, 0, 0);
    }

    public boolean isBorderOpaque() {
        return true;
    }

    public void zoomMas() {
        contador++;
        if (contador == 0) {
            mapa = original;
        } else if (contador == -1) {
            mapa = rescaleImage(original, (int) (original.getWidth(null) * 0.75), (int) (original.getHeight(null) * 0.75));
        } else if (contador == 1) {
            mapa = rescaleImage(original, (int) (original.getWidth(null) * 1.5), (int) (original.getHeight(null) * 1.5));
        } else if (contador == 2) {
            mapa = rescaleImage(original, (int) (original.getWidth(null) * 3), (int) (original.getHeight(null) * 3));
        } else {
            contador--;
        }
        displayMapa.getGraphics().clearRect(0,0,displayMapa.getWidth(),displayMapa.getHeight());
        paintBorder(null, displayMapa.getGraphics(), 0, 0, 0, 0);
    }

    public void zoomMenos() {
        contador--;
        if (contador == 0) {
            mapa = original;
        } else if (contador == 1) {
            mapa = rescaleImage(original, (int) (original.getWidth(null) * 1.5), (int) (original.getHeight(null) * 1.5));
        } else if (contador == -1) {
            mapa = rescaleImage(original, (int) (original.getWidth(null) * 0.75), (int) (original.getHeight(null) * 0.75));
        } else if (contador == -2) {
            mapa = rescaleImage(original, (int) (original.getWidth(null) * 0.325), (int) (original.getHeight(null) * 0.325));
        } else {
            contador++;
        }
        displayMapa.getGraphics().clearRect(0,0,displayMapa.getWidth(),displayMapa.getHeight());
        paintBorder(null, displayMapa.getGraphics(), 0, 0, 0, 0);
    }
    /*
    protected void keyPressed(int keyCode) {
    switch (keyCode) {
    case -1:    //ARRIBA
    despY += 5;
    repaint();
    break;
    case -2:
    despY -= 5;
    repaint();
    break;
    case -3:
    despX += 5;
    repaint();
    break;
    case -4:
    despX -= 5;
    repaint();
    break;
    case -5:
    mapa = rescaleImage(mapa, (int) (mapa.getWidth() * 1.5), (int) (mapa.getHeight() * 1.5));
    repaint();
    break;
    }
    }
     */

    private void paintNotFound(Graphics g) {
        //Establecemos el color de fondo y reiniciamos la pantalla
        g.setColor(new Color(0x000000));
        g.fillRect(0, 0, getWidth(), getHeight());
        for (int i = 0; i <= displayMapa.getWidth() / nomap.getWidth(null); i++) {
            for (int j = 0; j <= getHeight() / nomap.getHeight(null); j++) {
                g.drawImage(nomap, i * nomap.getWidth(null), j * nomap.getHeight(null), null);
            }
        }
    }
    /*
    private Image rescaleImage(Image image, int width, int height) {
    int sourceWidth = image.getWidth(null);
    int sourceHeight = image.getHeight(null);

    Image newImage = new Image(width, height);
    Graphics g = newImage.getGraphics();

    for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
    g.setClip(x, y, 1, 1);
    int dx = x * sourceWidth / width;
    int dy = y * sourceHeight / height;
    g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);
    }
    }

    return Image.createImage(newImage);
    }*/

    private Image rescaleImage(Image source, int width, int height) {
        Image rescale=null;
        MediaTracker media = new MediaTracker(this);
        // java how-to image for example, can be JPG
        media.addImage(source, 0);
        try {
            media.waitForID(0);
            // scale down, half the original size
            ImageFilter replicate =
                    new ReplicateScaleFilter(width, height);
            ImageProducer prod =
                    new FilteredImageSource(source.getSource(), replicate);
            rescale = createImage(prod);
            media.addImage(rescale, 1);
            media.waitForID(1);
        } catch (InterruptedException e) {
        }
        return rescale;
    }
}


